
package service.impl;

import service.UserService;
import util.MD5Util;
import entity.User;
import dao.BaseDAO;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService{
	//依赖注入DAO
	@Resource(name="baseDAO")
	private BaseDAO baseDAO;
	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public User login(User user) {
		String hql = null;
		String passWord_md5 = null;
		try {
			passWord_md5 = MD5Util.getMD5(user.getPassWord());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(user.getUserName() != null) {
			hql = "from User as a where a.userName='"+user.getUserName()+"' and a.passWord_md5='"+passWord_md5+"'";
		}else {
			hql = "from User as a where a.email='"+user.getEmail()+"' and a.passWord_md5='"+passWord_md5+"'";
		}		
		User u = (User)baseDAO.loadObject(hql);
		if( u != null) return u;
		return null;
	}
	
	public boolean reg(User user) {	
		user.setPassWord_md5(MD5Util.getMD5(user.getPassWord()));
		baseDAO.saveOrUpdate(user);
		return true;
	}
	
	public boolean check(String param,int type) {
		String hql = null;
		if(1 == type) {
			hql = "from User u where u.userName = '"+param+"'";
		}else {
			hql = "from User u where u.email ='"+param+"'";
		}
		if(baseDAO.loadObject(hql) != null) {
			return false;
		}
		return true;
	}
	
}
